//
//  BaseChartViewController.m
//  Stock
//
//  Created by blacksun on 11-6-21.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "BaseChartViewController.h"
#import "StockListController.h"


@implementation BaseChartViewController

@synthesize panrentController;
@synthesize imageView;
@synthesize chartType;
@synthesize imageDownloader;
@synthesize currentMyStock;
@synthesize index;
@synthesize imageTime;
@synthesize remoteTimeStamp;

-(void)chartViewDidDisplay{
    if (isDownLoading) {
        return;
    }
    if (!self.imageView.image ||![imageTime.longTime isEqualToString:remoteTimeStamp]) {
        NSString *imageUrl = [NSString stringWithFormat:@"%@stock/chart?id=%@&type=%@",kHostName,currentMyStock.uuid,chartType];
        NSLog(@"%@",imageUrl);
        [self.imageDownloader downLoadImageWithUrl:imageUrl];
        isDownLoading = YES;
    }
}


-(void)requestFinishedWithResult:(id)aResult andUserinfo:(NSDictionary *)aUserinfo{
    if (aResult) {
        NSData *imageData = UIImagePNGRepresentation(aResult);
        NSString *imageFilePath = [[CommonUtil shareInstance] pathOfStockImageFile:[NSString stringWithFormat:@"%@_%@.jpg",currentMyStock.uuid,chartType]];
        [imageData writeToFile:imageFilePath atomically:YES];
        
        self.imageTime.longTime = remoteTimeStamp;
        [imageTime save];
    }
    isDownLoading = NO;
}
-(void)requestFinishedwithError:(NSError *)aError andUserinfo:(NSDictionary *)aUserinfo{
    isDownLoading = NO;
}


- (void)dealloc
{
    self.chartType = nil;
    self.imageView = nil;
    self.imageTime = nil;
    [self.imageDownloader cancleAllRequest];
    self.imageDownloader = nil;
    self.currentMyStock = nil;
    self.remoteTimeStamp = nil;
    [panrentController.parentController.updateSuccessNotification removeObserver:self];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    [self.imageDownloader cancleAllRequest];
    
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.frame = CGRectMake(320.0*index, 0.0, 320.0, 120.0);
    
    self.imageDownloader = [[[DownloadImageService alloc] initWithDelegate:self] autorelease];
    NSString *imagePath = [[CommonUtil shareInstance] pathOfStockImageFile:[NSString stringWithFormat:@"%@_%@.jpg",currentMyStock.uuid,chartType]];
    UIImage *image = [[UIImage alloc] initWithContentsOfFile:imagePath];
    self.imageView.image = image;
    StockImageTime *tempTime = [StockImageTime findByStockId:currentMyStock.uuid andImageType:chartType];
    self.imageTime = tempTime;
    if (!image || ![imageTime.longTime isEqualToString:remoteTimeStamp]) {
        [self chartViewDidDisplay];
    }
    [image release];
    
    [panrentController.parentController.updateSuccessNotification addObserver:self selector:@selector(chartViewDidDisplay) name:kUpdateSuccessNotification object:nil];
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    self.imageView = nil;
    [self.imageDownloader cancleAllRequest];
    self.imageTime = nil;
}
@end
